package com.test.host

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import com.imagepreview.plugin.FlutterEngineDelegate
import com.imagepreview.plugin.HomeActivity
import com.imagepreview.plugin.ImagePreviewActivity
import com.test.host.model.TestData
import com.test.host.ui.theme.ImagePreviewHostTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            ImagePreviewHostTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    ImageList(
                        modifier = Modifier.padding(innerPadding)
                    )
                }
            }
        }
    }
}


@Composable
fun ImageList(modifier: Modifier = Modifier) {
    val imageUrls = TestData.imageList

    LazyColumn(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp)
    ) {
        itemsIndexed(imageUrls) { index, imageUrl ->
            ImageCard(imageUrl = imageUrl, index = index)
        }
    }
}


@Composable
fun ImageCard(imageUrl: String, index: Int) {
    val context = LocalContext.current  // 获取当前上下文
    Card(
        modifier = Modifier
            .fillMaxWidth()
            .height(200.dp)
            .padding(vertical = 8.dp)
            .clickable { /* Handle click */
                // 路由跳转flutter AppFlutterActivity
                // 对3取余数为0
                if (index % 3 == 0) {
                    val intent = FlutterEngineDelegate.createIntent(context, "/home")
                    intent.setClass(context, HomeActivity::class.java)
                    context.startActivity(intent)
                } else {
                    val intent = FlutterEngineDelegate.createIntent(context, "/image/preview")
                    intent.setClass(context, ImagePreviewActivity::class.java)
                    intent.putStringArrayListExtra("images", ArrayList(TestData.imageList))
                    intent.putExtra("index", index)
                    context.startActivity(intent)
                }
            },
        elevation = CardDefaults.cardElevation(defaultElevation = 4.dp)
    ) {
        Column(
            modifier = Modifier.padding(16.dp)
        ) {
            AsyncImage(
                model = imageUrl,
                contentDescription = null,
                modifier = Modifier
                    .fillMaxWidth()
                    .height(150.dp)
            )
            Text(text = "Image from $imageUrl")
        }
    }
}